//
//  SXTLandingViewController.m
//  SXT
//
//  Created by 赵金鹏 on 16/8/18.
//  Copyright © 2016年 赵金鹏. All rights reserved.
//

#import "SXTLandingViewController.h"
#import "SXTLandingInPutView.h"//用户名密码输入view
#import "SXTLandingNextViewController.h"//注册页面下一步操作
#import "SXTLoginViewController.h"

//#import "UMSocial.h"//引入友盟的qq登陆
@interface SXTLandingViewController ()

@property (strong, nonatomic)   SXTLandingInPutView *landingView;              /** 输入view */

@end

@implementation SXTLandingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = MainColor;
    self.edgesForExtendedLayout = 0;
    self.title = @"注册";
    [self addController];

    if(self.isLoginPushToRegist){
        _landingView.goLoginBtn.hidden = YES;
    }
}
//添加控件
- (void)addController{
    [self.view addSubview:self.landingView];
    __weak typeof (self) weakSelf = self;
    [_landingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@230);
        make.left.right.top.equalTo(weakSelf.view);
    }];

}

- (SXTLandingInPutView *)landingView{
    if (!_landingView) {
        _landingView = [[SXTLandingInPutView alloc]init];
        __weak typeof (self) weakSelf = self;
        _landingView.nextBlock = ^(NSDictionary *dic){
//            SXTLandingNextViewController *next = [[SXTLandingNextViewController alloc]init];
//            next.userMessageDic = dic;
//            [weakSelf.navigationController pushViewController:next animated:YES];
            //完成注册
            //key=cp&action=reg&tel=12&password=12
            NSString *userName = dic[@"userName"];
            NSString *password = dic[@"password"];

            if (userName.length<11) {
                [weakSelf.view makeToast:@"手机号码错误" duration:0.5 position:CSToastPositionCenter];
                return ;
            }
            
            if (![userName isPhone]) {
                [weakSelf.view makeToast:@"手机号码错误" duration:0.5 position:CSToastPositionCenter];
                return ;
            }
            
            if (password.length<6) {
                [weakSelf.view makeToast:@"密码错误" duration:0.5 position:CSToastPositionCenter];
                return ;
            }
            
            NSMutableDictionary *params = [NSMutableDictionary dictionary];
            params[@"key"] = @"cp";
            params[@"action"] = @"reg";
            params[@"tel"] = userName;
            params[@"password"] = password;
            [weakSelf postData:@"http://guleiba.com/api/caipiao/user/" param:params success:^(id responseObject) {
                NSLog(@"%@",responseObject);
                //注册成功
                if ([responseObject[@"code"] integerValue] == 1) {
                    
                    [weakSelf showTostMessage:@"恭喜您,注册成功"];
                    //发布注册成功通知
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"RegistSuccess" object:nil userInfo:dic];
//                    //根据用户名的KEY保存
//                    //先保存用户名
//                    [[NSUserDefaults standardUserDefaults] setObject:dic[@"userName"] forKey:@"username"];
//                    //根据用户名保存用户数据
//                    [[NSUserDefaults standardUserDefaults] setObject:dic forKey:dic[@"userName"]];
                    [weakSelf performSelector:@selector(popMyViewController) withObject:nil afterDelay:1.0];
                }else{
                    [weakSelf showTostMessage:responseObject[@"msg"]];
//                    //注册失败  保存数据到本地
//                    [[NSNotificationCenter defaultCenter] postNotificationName:@"RegistSuccess" object:nil userInfo:dic];
//                    //根据用户名的KEY保存
//                    //先保存用户名
//                    [[NSUserDefaults standardUserDefaults] setObject:dic[@"userName"] forKey:@"username"];
//                    //根据用户名保存用户数据
//                    [[NSUserDefaults standardUserDefaults] setObject:dic forKey:dic[@"userName"]];
//                    [weakSelf performSelector:@selector(popMyViewController) withObject:nil afterDelay:1.0];
                }
            } error:^(NSError *error) {
                [weakSelf showTostMessage:error.domain];
//                ////注册失败  保存数据到本地
//                [[NSNotificationCenter defaultCenter] postNotificationName:@"RegistSuccess" object:nil userInfo:dic];
//                //根据用户名的KEY保存
//                //先保存用户名
//                [[NSUserDefaults standardUserDefaults] setObject:dic[@"userName"] forKey:@"username"];
//                //根据用户名保存用户数据
//                [[NSUserDefaults standardUserDefaults] setObject:dic forKey:dic[@"userName"]];
//                [weakSelf performSelector:@selector(popMyViewController) withObject:nil afterDelay:1.0];
            }];
        };
        [_landingView.goLoginBtn addTarget:self action:@selector(goLoginBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _landingView;
}

-(void)popMyViewController{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark======去登陆======
-(void)goLoginBtnClick:(UIButton *)btn{
    SXTLoginViewController *loginvc = [[SXTLoginViewController alloc] init];
    loginvc.isRegistPushToLogin = YES;
    [self.navigationController pushViewController:loginvc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
